//-----------------------------------------------------------------------
// <copyright file="EventBrokerExtension.cs" company="iron9light">
//      Copyright (c) iron9light. All rights reserved.
// </copyright>
// <author>iron9light</author>
//-----------------------------------------------------------------------

namespace EventBrokerExtension
{
    using EventBroker;

    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.ObjectBuilder;

    /// <summary>
    /// The event broker extension.
    /// </summary>
    public class EventBrokerExtension : UnityContainerExtension, IEventBrokerConfiguration
    {
        private IEventBroker eventBroker;

        /// <summary>
        /// Gets the event broker.
        /// </summary>
        /// <value>The event broker.</value>
        public IEventBroker EventBroker
        {
            get { return this.eventBroker; }
        }

        public override void Remove()
        {
            this.eventBroker.Dispose();
            base.Remove();
        }

        protected override void Initialize()
        {
            this.eventBroker = new EventBroker();
            
            this.Context.Locator.Add(typeof(IEventBroker), this.eventBroker);

            this.Context.Strategies.AddNew<PublicationWireupStrategy>(UnityBuildStage.Initialization);
            this.Context.Strategies.AddNew<SubscriptionWireupStrategy>(UnityBuildStage.Initialization);

            this.Context.Policies.SetDefault<IPublicationSelectorPolicy>(new DefaultPublicationSelectorPolicy());
            this.Context.Policies.SetDefault<ISubscriptionSelectorPolicy>(new DefaultSubscriptionSelectorPolicy());
        }
    }
}